package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.Register;
import org.rcloud.io.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2019/1/26.
 */

@RestController
@RequestMapping(value = "/api")
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    /*
    * 保存报名信息
    * */
    @ApiOperation(value="保存农户用户", notes="根据User对象保存用户")
    @ResponseBody
    @RequestMapping(value = "/save-registerinfo", produces = {"application/json;charset=UTF-8"})
    public ResultVo saveUser(@RequestBody String params){
        String reg = JSON.parseObject(params).getString("register");
        Register register = JSON.parseObject(reg, Register.class);

        return registerService.saveUser(register);

    }

      /*
     * 查询报名信息
     * */
    @ApiOperation(value="获取报名详细信息", notes="根据url的id来获取报名详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @ResponseBody
    @RequestMapping(value = "/find-registerinfo", produces = {"application/json;charset=UTF-8"})
    public ResultVo getUser(@RequestBody String params){
        System.out.println(params);
        JSONObject object= JSON.parseObject(params);
        return registerService.findApplyFromId(object.getInteger("id"));
    }

    /*
     * 图片上传
     * */
  /*  @ApiOperation(value="图片上传", notes="图片上传")
    @ResponseBody
    @RequestMapping(value = "/upload", produces = {"application/json;charset=UTF-8"})
    public String uplaod(HttpServletRequest req, @RequestParam("file") MultipartFile file, Model m) {
        //1. 接受上传的文件  @RequestParam("file") MultipartFile file
        try {
            //2.根据时间戳创建新的文件名，这样即便是第二次上传相同名称的文件，也不会把第一次的文件覆盖了
            String fileName = System.currentTimeMillis() + file.getOriginalFilename();
            //3.通过req.getServletContext().getRealPath("") 获取当前项目的真实路径，然后拼接前面的文件名
            String destFileName = req.getServletContext().getRealPath("") + "uploaded" + File.separator + fileName;
//            System.out.println(destFileName);
            //4.第一次运行的时候，这个文件所在的目录往往是不存在的，这里需要创建一下目录（创建到了webapp下uploaded文件夹下）
            File destFile = new File(destFileName);
            destFile.getParentFile().mkdirs();
            //5.把浏览器上传的文件复制到希望的位置
            file.transferTo(destFile);
            //6.把文件名放在model里，以便后续显示用
            m.addAttribute("fileName", fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "上传失败," + e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败," + e.getMessage();
        }

        return "success";
    }*/
}
